package springboot_shiro.com.ljs.config;

import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration//配置类
public class ShiroConfig {
    //
    @Bean("myrealm")
    public  Myrealm myrealm(){
        return  new Myrealm();
    }
    //myrealm加入到安全管理器
    @Bean("SecurityManager")
    public DefaultWebSecurityManager  securityManager(@Qualifier("myrealm") Myrealm myrealm){
        DefaultWebSecurityManager SecurityManager=new DefaultWebSecurityManager();
        SecurityManager.setRealm(myrealm);
        return  SecurityManager;
    }
    //securityManager加入到ShiroFilterFactoryBean

    @Bean("shiroFilterFactoryBean")
    public ShiroFilterFactoryBean ShiroFilterFactoryBean(@Qualifier("SecurityManager") DefaultWebSecurityManager  securityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean=new ShiroFilterFactoryBean();

        shiroFilterFactoryBean.setSecurityManager(securityManager);
        Map<String,String> map=new HashMap<>() ;
//创建权限过滤器
        map.put("/main","authc");
        map.put("/manage","perms[manage]");//manage的权限
        map.put("/admin","roles[admin]");//是admin的角色
        map.put("/druid/**", "anon");
        map.put("/rap", "anon");
 /*     map.put("/**", "user");*/
      /*  anon：无需认证。
        authc：必须认证。
        authcBasic：需要通过 HTTPBasic 认证。
        user：不一定通过认证，只要曾经被 Shiro 记录即可，比如：记住我。
        授权过滤器
        perms：必须拥有某个权限才能访问。
        role：必须拥有某个角色才能访问*/
        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
        shiroFilterFactoryBean.setLoginUrl("/tologin");
         shiroFilterFactoryBean.setSuccessUrl("/");
        shiroFilterFactoryBean.setUnauthorizedUrl("/unauthor");//未授权的话，再写个方法*/
        return  shiroFilterFactoryBean;
    }
    @Bean
    public ShiroDialect shiroDialect(){
       return new ShiroDialect();
    }
}
